﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MopsesPetShop
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            back.Visible = false;
            if (Session["backList"] == null)
                Session.Add("backList", new List<string>());

            //if (Request["back"] == null)
            (Session["backList"] as List<string>).Insert(0, Request.RawUrl);
            //else
            if ((Request["back"] != null))
            {
                (Session["backList"] as List<string>).RemoveAt(0);
                (Session["backList"] as List<string>).RemoveAt(0);
            }

            if ((Session["backList"] as List<string>).Count > 1)
            {
                back.NavigateUrl = (Session["backList"] as List<string>)[1] +((Session["backList"] as List<string>)[1].IndexOf('?')<0?"?":"&")+ "back=true";
                back.Visible = true;
            }

            var dc = new DataClasses1DataContext();
            foreach (var advertisment in dc.Advertisment)
            {
                var box = new HtmlGenericControl();
                var img = new HtmlImage
                {
                    Src = "ImageHandler.ashx?id=" + advertisment.ekPreview,
                    Alt = ""
                };
                img.Style.Add(HtmlTextWriterStyle.Width, "120px");
                box.Controls.Add(img);
                var text = new HtmlGenericControl
                {
                    InnerText = advertisment.message
                };
                text.Style.Add(HtmlTextWriterStyle.Display, "block");
                text.Style.Add(HtmlTextWriterStyle.Width, "120px");
                box.Controls.Add(text);
                var hlink = new HyperLink
                {
                    Text = advertisment.hyperlink,
                    NavigateUrl = advertisment.hyperlink
                };
                hlink.Style.Add(HtmlTextWriterStyle.Width, "120px");
                box.Controls.Add(hlink);
                box.Style.Add(HtmlTextWriterStyle.Display, "block");
                box.Style.Add(HtmlTextWriterStyle.MarginTop, "50px");
                advertismentDiv.Controls.Add(box);
            }

            if (!IsPostBack)
                foreach (var entity in dc.Entities)
                {
                    var file = "ImageHandler.ashx?id=" + entity.ekPreview + "&sizeW=400&sizeH=300";
                    var mi = new MenuItem("", "", file, "Default.aspx?entity=" + entity.entity + "&id=" + entity.ekPreview + "&sizeW=400&sizeH=300");
                    mainMenu.Items.Add(mi);
                }
        }
    }
}